package kjx_test_security.auth.components;


import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @ClassName:
 * @Author:k1919
 * 用于处理用户已经登录没有权限访问资源
 **/

@Slf4j
public class ResetAccessDeniedHandler implements AccessDeniedHandler  {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException {
        String message = accessDeniedException.getMessage();
        System.out.println("SSSSSSSSSSSSSSSSSS");
        log.error("授权失败: {}", message);
        response.setCharacterEncoding("UTF-8");
        response.sendError(1113, HttpStatus.FORBIDDEN.getReasonPhrase());
        ServletUtil.write(response,"<body style=\"background-color: #abcdef;\">  <h1 style='color:red'>您暂无权限acc</h1></body>","text/html");
    }
}
